package com.hckjsec.system.controller;

import java.util.List;

import com.hckjsec.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.hckjsec.common.annotation.Log;
import com.hckjsec.common.core.controller.BaseController;
import com.hckjsec.common.core.domain.AjaxResult;
import com.hckjsec.common.enums.BusinessType;
import com.hckjsec.system.domain.SysReceipt;
import com.hckjsec.system.service.ISysReceiptService;
import com.hckjsec.common.utils.poi.ExcelUtil;
import com.hckjsec.common.core.page.TableDataInfo;

/**
 * 收文Controller
 * 
 * @author cyt
 * @date 2021-10-11
 */
@RestController
@RequestMapping("/system/receipt")
public class SysReceiptController extends BaseController
{
    @Autowired
    private ISysReceiptService sysReceiptService;

    /**
     * 查询收文列表
     */
    @GetMapping("/list")
    public TableDataInfo list(SysReceipt sysReceipt)
    {
        startPage();
        if(!SecurityUtils.isAdmin(SecurityUtils.getUserId())){
            sysReceipt.setDeptId(SecurityUtils.getDeptId());
        }
        List<SysReceipt> list = sysReceiptService.selectSysReceiptList(sysReceipt);
        return getDataTable(list);
    }

    /**
     * 导出收文列表
     */
    @Log(title = "收文", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SysReceipt sysReceipt)
    {
        if(!SecurityUtils.isAdmin(SecurityUtils.getUserId())){
            sysReceipt.setDeptId(SecurityUtils.getDeptId());
        }
        List<SysReceipt> list = sysReceiptService.selectSysReceiptList(sysReceipt);
        ExcelUtil<SysReceipt> util = new ExcelUtil<SysReceipt>(SysReceipt.class);
        return util.exportExcel(list, "receipt");
    }

    /**
     * 获取收文详细信息
     */
    @GetMapping(value = "/{receiptId}")
    public AjaxResult getInfo(@PathVariable("receiptId") Long receiptId)
    {
        return AjaxResult.success(sysReceiptService.selectSysReceiptById(receiptId));
    }

    /**
     * 新增收文
     */
    @Log(title = "收文", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysReceipt sysReceipt)
    {
        sysReceipt.setDeptId(SecurityUtils.getDeptId());
        sysReceipt.setOperator(SecurityUtils.getUserId());
        return toAjax(sysReceiptService.insertSysReceipt(sysReceipt));
    }

    /**
     * 修改收文
     */
    @Log(title = "收文", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysReceipt sysReceipt)
    {
        return toAjax(sysReceiptService.updateSysReceipt(sysReceipt));
    }

    /**
     * 删除收文
     */
    @Log(title = "收文", businessType = BusinessType.DELETE)
	@DeleteMapping("/{receiptIds}")
    public AjaxResult remove(@PathVariable Long[] receiptIds)
    {
        return toAjax(sysReceiptService.deleteSysReceiptByIds(receiptIds));
    }
}
